Life of the game
Life.h
#ifndef LIFE_H
#define LIFE_H
#include <iostream>
#include <cstdlib>
#include <time.h>
const int maxrow = 20,maxcol = 60;
typedef struct cell
{
  int flag;//0表示死亡，1表示生存；
  int count = 0;
}CELL;

class Life
{
    public:
        Life();
        virtual ~Life();
        int user_says();
        void initialize();//初始化配置
        void print();//输出当前配置
        void update();//一次迭代

    protected:

    private:
        CELL grid[maxrow+2][maxcol+2];
        //allows for tow extra rows and columns
        void neighbor_count(int row,int col);
};
#endif // LIFE_H
Life.cpp
#include "Life.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
Life::Life()
{
    //ctor
}

Life::~Life()
{
    //dtor
}
void Life::initialize()
    {
        srand(unsigned(time));
        int i,j;
        for(i=0;i<maxrow;i++)
        {
            for(j=0;j<maxcol;j++)
            {
                grid[i][j].flag = rand()%2;
            }
        }
    }
void Life::print()
    {
        int i,j;
        for(i=0;i<maxrow;i++)
        {
            for(j=0;j<maxcol;j++)
            {
                if(grid[i][j].flag)
                    cout<<'*';
                else
                    cout<<'.';
            }
            cout<<endl;
        }
    }

void Life::update()
    {
        neighbor_count(maxrow,maxcol);
        int i,j;
        for(i=0;i<maxrow;i++)
        {
            for(j=0;j<maxcol;j++)
            {
                if(grid[i][j].flag==1&&grid[i][j].count<=1)
                    grid[i][j].flag = 0;
                else if(grid[i][j].flag==1&&grid[i][j].count>=4)
                    grid[i][j].flag=0;
                else if(grid[i][j].flag==1&&(grid[i][j].count==2||grid[i][j].count==3))
                    grid[i][j].flag =1;
                else if(grid[i][j].flag==0&&grid[i][j].count==3)
                    grid[i][j].flag = 1;
            }
        }
    }

void Life::neighbor_count(int row,int col)
    {
        int i,j;
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                grid[i][j].count = 0;
            }
        }
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                if(i-1>=0&&j>=0&&grid[i-1][j].flag)
                        grid[i][j].count++;
                if(i-1>=0&&j-1>=0&&grid[i-1][j-1].flag)
                    grid[i][j].count++;
                if(i-1>=0&&j+1<col&&grid[i-1][j+1].flag)
                    grid[i][j].count++;
                if(i>=0&&j-1>=0&&grid[i][j-1].flag)
                    grid[i][j].count++;
                if(i>=0&&j+1<col&&grid[i][j+1].flag)
                    grid[i][j].count++;
                if(i+1<row&&j>=0&&grid[i+1][j].flag)
                    grid[i][j].count++;
                if(i+1<row&&j-1>=0&&grid[i+1][j-1].flag)
                    grid[i][j].count++;
                if(i+1<row&&j+1<col&&grid[i+1][j+1].flag)
                    grid[i][j].count++;
            }
        }
    }
int Life::user_says()
{
    char ch;
    cin>>ch;
    if(ch=='y')
        return 1;
    else if(ch=='n')
        return 0;
}
main.cpp
#include "Life.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
    Life configuration;
    configuration.initialize();
    configuration.print();
    cout<<"Continue viewing new generations?"<<endl;
    while(configuration.user_says())
    {
        configuration.update();
        configuration.print();
        cout<<"Continue viewing new generations?"<<endl;
    }
    return 0;
}
